﻿
Imports System.Net
Imports std

Namespace netstat
    Public Class tcp_row
        Private _local As IPEndPoint = Nothing
        Private _remote As IPEndPoint = Nothing

        Public Sub New(ByVal mibrow As MIB_TCPROW)
            _local = New IPEndPoint(mibrow.dwLocalAddr, mibrow.dwLocalPort)
            _remote = New IPEndPoint(mibrow.dwRemoteAddr, mibrow.dwRemotePort)
        End Sub

        Public Function local() As IPEndPoint
            Return _local
        End Function

        Public Function remote() As IPEndPoint
            Return _remote
        End Function
    End Class

    Public Class tcp_table
        Inherits vector(Of tcp_row)
    End Class

    Public Module _netstat
        Private _tcptable As tcp_table = Nothing

        Public Function tcptable() As tcp_table
            Return _tcptable
        End Function

        Public Function refreshTcpTable() As Boolean
            If _tcptable Is Nothing Then
                _tcptable = New tcp_table()
            Else
                _tcptable.clear()
            End If

            If refreshMIBTcpTable() AndAlso Not MIBTcpTable().table Is Nothing Then
                Dim i As Int64
                For i = 0 To MIBTcpTable().table.Length - 1
                    _tcptable.push_back(New tcp_row(MIBTcpTable().table(i)))
                Next

                Return True
            Else
                Return False
            End If
        End Function
    End Module
End Namespace
